/* eslint-disable */
import Long from "long";
import _m0 from "protobufjs/minimal";
import { Empty } from "../google/protobuf/empty";
import { FieldMask } from "../google/protobuf/field_mask";
import { Timestamp } from "../google/protobuf/timestamp";

export const protobufPackage = "bytebase.v1";

export interface CreateWorksheetRequest {
  /** The worksheet to create. */
  worksheet: Worksheet | undefined;
}

export interface GetWorksheetRequest {
  /**
   * The name of the worksheet to retrieve.
   * Format: worksheets/{worksheet}
   */
  name: string;
}

export interface UpdateWorksheetRequest {
  /**
   * The worksheet to update.
   *
   * The worksheet's `name` field is used to identify the worksheet to update.
   * Format: worksheets/{worksheet}
   */
  worksheet:
    | Worksheet
    | undefined;
  /**
   * The list of fields to be updated.
   * Fields are specified relative to the worksheet.
   * (e.g. `title`, `statement`; *not* `worksheet.title` or `worksheet.statement`)
   * Only support update the following fields for now:
   * - `title`
   * - `statement`
   * - `starred`
   * - `visibility`
   */
  updateMask: string[] | undefined;
}

export interface UpdateWorksheetOrganizerRequest {
  /**
   * The organizer to update.
   *
   * The organizer's `worksheet` field is used to identify the worksheet.
   * Format: worksheets/{worksheet}
   */
  organizer:
    | WorksheetOrganizer
    | undefined;
  /**
   * The list of fields to be updated.
   * Fields are specified relative to the worksheet organizer.
   * Only support update the following fields for now:
   * - `starred`
   */
  updateMask: string[] | undefined;
}

export interface WorksheetOrganizer {
  /**
   * The name of the worksheet.
   * Format: worksheets/{worksheet}
   */
  worksheet: string;
  /** starred means if the worksheet is starred. */
  starred: boolean;
}

export interface DeleteWorksheetRequest {
  /**
   * The name of the worksheet to delete.
   * Format: worksheets/{worksheet}
   */
  name: string;
}

export interface SearchWorksheetsRequest {
  /**
   * To filter the search result.
   * Format: only support the following spec for now:
   * - `creator = users/{email}`, `creator != users/{email}`
   * - `starred = true`, `starred = false`.
   * - `visibility = "VISIBILITY_PRIVATE"`, `visibility = "VISIBILITY_PROJECT_READ | VISIBILITY_PROJECT_WRITE"`, etc.
   * Not support empty filter for now.
   */
  filter: string;
  /**
   * Not used. The maximum number of worksheets to return. The service may return fewer than
   * this value.
   * If unspecified, at most 50 worksheets will be returned.
   * The maximum value is 1000; values above 1000 will be coerced to 1000.
   */
  pageSize: number;
  /**
   * Not used. A page token, received from a previous `SearchWorksheets` call.
   * Provide this to retrieve the subsequent page.
   *
   * When paginating, all other parameters provided to `SearchWorksheets` must match
   * the call that provided the page token.
   */
  pageToken: string;
}

export interface SearchWorksheetsResponse {
  /** The worksheets that matched the search criteria. */
  worksheets: Worksheet[];
  /**
   * Not used. A token, which can be sent as `page_token` to retrieve the next page.
   * If this field is omitted, there are no subsequent pages.
   */
  nextPageToken: string;
}

export interface Worksheet {
  /**
   * The name of the worksheet resource, generated by the server.
   * Canonical parent is project.
   * Format: worksheets/{worksheet}
   */
  name: string;
  /**
   * The project resource name.
   * Format: projects/{project}
   */
  project: string;
  /**
   * The database resource name.
   * Format: instances/{instance}/databases/{database}
   * If the database parent doesn't exist, the database field is empty.
   */
  database: string;
  /** The title of the worksheet. */
  title: string;
  /**
   * The creator of the Worksheet.
   * Format: users/{email}
   */
  creator: string;
  /** The create time of the worksheet. */
  createTime:
    | Date
    | undefined;
  /** The last update time of the worksheet. */
  updateTime:
    | Date
    | undefined;
  /**
   * The content of the worksheet.
   * By default, it will be cut off in SearchWorksheet() method. If it doesn't match the `content_size`, you can
   * use GetWorksheet() request to retrieve the full content.
   */
  content: Uint8Array;
  /** content_size is the full size of the content, may not match the size of the `content` field. */
  contentSize: Long;
  visibility: Worksheet_Visibility;
  /** starred indicates whether the worksheet is starred by the current authenticated user. */
  starred: boolean;
}

export enum Worksheet_Visibility {
  VISIBILITY_UNSPECIFIED = "VISIBILITY_UNSPECIFIED",
  /** VISIBILITY_PROJECT_READ - Read access in project scope, worksheet OWNER/DBA and project OWNER can read/write, other project members can read. */
  VISIBILITY_PROJECT_READ = "VISIBILITY_PROJECT_READ",
  /** VISIBILITY_PROJECT_WRITE - Write access in project scope, worksheet OWNER/DBA and all members in the project can write the worksheet. */
  VISIBILITY_PROJECT_WRITE = "VISIBILITY_PROJECT_WRITE",
  /** VISIBILITY_PRIVATE - Private, only worksheet OWNER can read/write. */
  VISIBILITY_PRIVATE = "VISIBILITY_PRIVATE",
  UNRECOGNIZED = "UNRECOGNIZED",
}

export function worksheet_VisibilityFromJSON(object: any): Worksheet_Visibility {
  switch (object) {
    case 0:
    case "VISIBILITY_UNSPECIFIED":
      return Worksheet_Visibility.VISIBILITY_UNSPECIFIED;
    case 1:
    case "VISIBILITY_PROJECT_READ":
      return Worksheet_Visibility.VISIBILITY_PROJECT_READ;
    case 2:
    case "VISIBILITY_PROJECT_WRITE":
      return Worksheet_Visibility.VISIBILITY_PROJECT_WRITE;
    case 3:
    case "VISIBILITY_PRIVATE":
      return Worksheet_Visibility.VISIBILITY_PRIVATE;
    case -1:
    case "UNRECOGNIZED":
    default:
      return Worksheet_Visibility.UNRECOGNIZED;
  }
}

export function worksheet_VisibilityToJSON(object: Worksheet_Visibility): string {
  switch (object) {
    case Worksheet_Visibility.VISIBILITY_UNSPECIFIED:
      return "VISIBILITY_UNSPECIFIED";
    case Worksheet_Visibility.VISIBILITY_PROJECT_READ:
      return "VISIBILITY_PROJECT_READ";
    case Worksheet_Visibility.VISIBILITY_PROJECT_WRITE:
      return "VISIBILITY_PROJECT_WRITE";
    case Worksheet_Visibility.VISIBILITY_PRIVATE:
      return "VISIBILITY_PRIVATE";
    case Worksheet_Visibility.UNRECOGNIZED:
    default:
      return "UNRECOGNIZED";
  }
}

export function worksheet_VisibilityToNumber(object: Worksheet_Visibility): number {
  switch (object) {
    case Worksheet_Visibility.VISIBILITY_UNSPECIFIED:
      return 0;
    case Worksheet_Visibility.VISIBILITY_PROJECT_READ:
      return 1;
    case Worksheet_Visibility.VISIBILITY_PROJECT_WRITE:
      return 2;
    case Worksheet_Visibility.VISIBILITY_PRIVATE:
      return 3;
    case Worksheet_Visibility.UNRECOGNIZED:
    default:
      return -1;
  }
}

function createBaseCreateWorksheetRequest(): CreateWorksheetRequest {
  return { worksheet: undefined };
}

export const CreateWorksheetRequest = {
  encode(message: CreateWorksheetRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.worksheet !== undefined) {
      Worksheet.encode(message.worksheet, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): CreateWorksheetRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseCreateWorksheetRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.worksheet = Worksheet.decode(reader, reader.uint32());
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): CreateWorksheetRequest {
    return { worksheet: isSet(object.worksheet) ? Worksheet.fromJSON(object.worksheet) : undefined };
  },

  toJSON(message: CreateWorksheetRequest): unknown {
    const obj: any = {};
    if (message.worksheet !== undefined) {
      obj.worksheet = Worksheet.toJSON(message.worksheet);
    }
    return obj;
  },

  create(base?: DeepPartial<CreateWorksheetRequest>): CreateWorksheetRequest {
    return CreateWorksheetRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<CreateWorksheetRequest>): CreateWorksheetRequest {
    const message = createBaseCreateWorksheetRequest();
    message.worksheet = (object.worksheet !== undefined && object.worksheet !== null)
      ? Worksheet.fromPartial(object.worksheet)
      : undefined;
    return message;
  },
};

function createBaseGetWorksheetRequest(): GetWorksheetRequest {
  return { name: "" };
}

export const GetWorksheetRequest = {
  encode(message: GetWorksheetRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.name !== "") {
      writer.uint32(10).string(message.name);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): GetWorksheetRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseGetWorksheetRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.name = reader.string();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): GetWorksheetRequest {
    return { name: isSet(object.name) ? globalThis.String(object.name) : "" };
  },

  toJSON(message: GetWorksheetRequest): unknown {
    const obj: any = {};
    if (message.name !== "") {
      obj.name = message.name;
    }
    return obj;
  },

  create(base?: DeepPartial<GetWorksheetRequest>): GetWorksheetRequest {
    return GetWorksheetRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<GetWorksheetRequest>): GetWorksheetRequest {
    const message = createBaseGetWorksheetRequest();
    message.name = object.name ?? "";
    return message;
  },
};

function createBaseUpdateWorksheetRequest(): UpdateWorksheetRequest {
  return { worksheet: undefined, updateMask: undefined };
}

export const UpdateWorksheetRequest = {
  encode(message: UpdateWorksheetRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.worksheet !== undefined) {
      Worksheet.encode(message.worksheet, writer.uint32(10).fork()).ldelim();
    }
    if (message.updateMask !== undefined) {
      FieldMask.encode(FieldMask.wrap(message.updateMask), writer.uint32(18).fork()).ldelim();
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): UpdateWorksheetRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseUpdateWorksheetRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.worksheet = Worksheet.decode(reader, reader.uint32());
          continue;
        case 2:
          if (tag !== 18) {
            break;
          }

          message.updateMask = FieldMask.unwrap(FieldMask.decode(reader, reader.uint32()));
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): UpdateWorksheetRequest {
    return {
      worksheet: isSet(object.worksheet) ? Worksheet.fromJSON(object.worksheet) : undefined,
      updateMask: isSet(object.updateMask) ? FieldMask.unwrap(FieldMask.fromJSON(object.updateMask)) : undefined,
    };
  },

  toJSON(message: UpdateWorksheetRequest): unknown {
    const obj: any = {};
    if (message.worksheet !== undefined) {
      obj.worksheet = Worksheet.toJSON(message.worksheet);
    }
    if (message.updateMask !== undefined) {
      obj.updateMask = FieldMask.toJSON(FieldMask.wrap(message.updateMask));
    }
    return obj;
  },

  create(base?: DeepPartial<UpdateWorksheetRequest>): UpdateWorksheetRequest {
    return UpdateWorksheetRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<UpdateWorksheetRequest>): UpdateWorksheetRequest {
    const message = createBaseUpdateWorksheetRequest();
    message.worksheet = (object.worksheet !== undefined && object.worksheet !== null)
      ? Worksheet.fromPartial(object.worksheet)
      : undefined;
    message.updateMask = object.updateMask ?? undefined;
    return message;
  },
};

function createBaseUpdateWorksheetOrganizerRequest(): UpdateWorksheetOrganizerRequest {
  return { organizer: undefined, updateMask: undefined };
}

export const UpdateWorksheetOrganizerRequest = {
  encode(message: UpdateWorksheetOrganizerRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.organizer !== undefined) {
      WorksheetOrganizer.encode(message.organizer, writer.uint32(10).fork()).ldelim();
    }
    if (message.updateMask !== undefined) {
      FieldMask.encode(FieldMask.wrap(message.updateMask), writer.uint32(18).fork()).ldelim();
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): UpdateWorksheetOrganizerRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseUpdateWorksheetOrganizerRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.organizer = WorksheetOrganizer.decode(reader, reader.uint32());
          continue;
        case 2:
          if (tag !== 18) {
            break;
          }

          message.updateMask = FieldMask.unwrap(FieldMask.decode(reader, reader.uint32()));
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): UpdateWorksheetOrganizerRequest {
    return {
      organizer: isSet(object.organizer) ? WorksheetOrganizer.fromJSON(object.organizer) : undefined,
      updateMask: isSet(object.updateMask) ? FieldMask.unwrap(FieldMask.fromJSON(object.updateMask)) : undefined,
    };
  },

  toJSON(message: UpdateWorksheetOrganizerRequest): unknown {
    const obj: any = {};
    if (message.organizer !== undefined) {
      obj.organizer = WorksheetOrganizer.toJSON(message.organizer);
    }
    if (message.updateMask !== undefined) {
      obj.updateMask = FieldMask.toJSON(FieldMask.wrap(message.updateMask));
    }
    return obj;
  },

  create(base?: DeepPartial<UpdateWorksheetOrganizerRequest>): UpdateWorksheetOrganizerRequest {
    return UpdateWorksheetOrganizerRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<UpdateWorksheetOrganizerRequest>): UpdateWorksheetOrganizerRequest {
    const message = createBaseUpdateWorksheetOrganizerRequest();
    message.organizer = (object.organizer !== undefined && object.organizer !== null)
      ? WorksheetOrganizer.fromPartial(object.organizer)
      : undefined;
    message.updateMask = object.updateMask ?? undefined;
    return message;
  },
};

function createBaseWorksheetOrganizer(): WorksheetOrganizer {
  return { worksheet: "", starred: false };
}

export const WorksheetOrganizer = {
  encode(message: WorksheetOrganizer, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.worksheet !== "") {
      writer.uint32(10).string(message.worksheet);
    }
    if (message.starred === true) {
      writer.uint32(16).bool(message.starred);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): WorksheetOrganizer {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseWorksheetOrganizer();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.worksheet = reader.string();
          continue;
        case 2:
          if (tag !== 16) {
            break;
          }

          message.starred = reader.bool();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): WorksheetOrganizer {
    return {
      worksheet: isSet(object.worksheet) ? globalThis.String(object.worksheet) : "",
      starred: isSet(object.starred) ? globalThis.Boolean(object.starred) : false,
    };
  },

  toJSON(message: WorksheetOrganizer): unknown {
    const obj: any = {};
    if (message.worksheet !== "") {
      obj.worksheet = message.worksheet;
    }
    if (message.starred === true) {
      obj.starred = message.starred;
    }
    return obj;
  },

  create(base?: DeepPartial<WorksheetOrganizer>): WorksheetOrganizer {
    return WorksheetOrganizer.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<WorksheetOrganizer>): WorksheetOrganizer {
    const message = createBaseWorksheetOrganizer();
    message.worksheet = object.worksheet ?? "";
    message.starred = object.starred ?? false;
    return message;
  },
};

function createBaseDeleteWorksheetRequest(): DeleteWorksheetRequest {
  return { name: "" };
}

export const DeleteWorksheetRequest = {
  encode(message: DeleteWorksheetRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.name !== "") {
      writer.uint32(10).string(message.name);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): DeleteWorksheetRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseDeleteWorksheetRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.name = reader.string();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): DeleteWorksheetRequest {
    return { name: isSet(object.name) ? globalThis.String(object.name) : "" };
  },

  toJSON(message: DeleteWorksheetRequest): unknown {
    const obj: any = {};
    if (message.name !== "") {
      obj.name = message.name;
    }
    return obj;
  },

  create(base?: DeepPartial<DeleteWorksheetRequest>): DeleteWorksheetRequest {
    return DeleteWorksheetRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<DeleteWorksheetRequest>): DeleteWorksheetRequest {
    const message = createBaseDeleteWorksheetRequest();
    message.name = object.name ?? "";
    return message;
  },
};

function createBaseSearchWorksheetsRequest(): SearchWorksheetsRequest {
  return { filter: "", pageSize: 0, pageToken: "" };
}

export const SearchWorksheetsRequest = {
  encode(message: SearchWorksheetsRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.filter !== "") {
      writer.uint32(10).string(message.filter);
    }
    if (message.pageSize !== 0) {
      writer.uint32(16).int32(message.pageSize);
    }
    if (message.pageToken !== "") {
      writer.uint32(26).string(message.pageToken);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): SearchWorksheetsRequest {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseSearchWorksheetsRequest();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.filter = reader.string();
          continue;
        case 2:
          if (tag !== 16) {
            break;
          }

          message.pageSize = reader.int32();
          continue;
        case 3:
          if (tag !== 26) {
            break;
          }

          message.pageToken = reader.string();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): SearchWorksheetsRequest {
    return {
      filter: isSet(object.filter) ? globalThis.String(object.filter) : "",
      pageSize: isSet(object.pageSize) ? globalThis.Number(object.pageSize) : 0,
      pageToken: isSet(object.pageToken) ? globalThis.String(object.pageToken) : "",
    };
  },

  toJSON(message: SearchWorksheetsRequest): unknown {
    const obj: any = {};
    if (message.filter !== "") {
      obj.filter = message.filter;
    }
    if (message.pageSize !== 0) {
      obj.pageSize = Math.round(message.pageSize);
    }
    if (message.pageToken !== "") {
      obj.pageToken = message.pageToken;
    }
    return obj;
  },

  create(base?: DeepPartial<SearchWorksheetsRequest>): SearchWorksheetsRequest {
    return SearchWorksheetsRequest.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<SearchWorksheetsRequest>): SearchWorksheetsRequest {
    const message = createBaseSearchWorksheetsRequest();
    message.filter = object.filter ?? "";
    message.pageSize = object.pageSize ?? 0;
    message.pageToken = object.pageToken ?? "";
    return message;
  },
};

function createBaseSearchWorksheetsResponse(): SearchWorksheetsResponse {
  return { worksheets: [], nextPageToken: "" };
}

export const SearchWorksheetsResponse = {
  encode(message: SearchWorksheetsResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    for (const v of message.worksheets) {
      Worksheet.encode(v!, writer.uint32(10).fork()).ldelim();
    }
    if (message.nextPageToken !== "") {
      writer.uint32(18).string(message.nextPageToken);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): SearchWorksheetsResponse {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseSearchWorksheetsResponse();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.worksheets.push(Worksheet.decode(reader, reader.uint32()));
          continue;
        case 2:
          if (tag !== 18) {
            break;
          }

          message.nextPageToken = reader.string();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): SearchWorksheetsResponse {
    return {
      worksheets: globalThis.Array.isArray(object?.worksheets)
        ? object.worksheets.map((e: any) => Worksheet.fromJSON(e))
        : [],
      nextPageToken: isSet(object.nextPageToken) ? globalThis.String(object.nextPageToken) : "",
    };
  },

  toJSON(message: SearchWorksheetsResponse): unknown {
    const obj: any = {};
    if (message.worksheets?.length) {
      obj.worksheets = message.worksheets.map((e) => Worksheet.toJSON(e));
    }
    if (message.nextPageToken !== "") {
      obj.nextPageToken = message.nextPageToken;
    }
    return obj;
  },

  create(base?: DeepPartial<SearchWorksheetsResponse>): SearchWorksheetsResponse {
    return SearchWorksheetsResponse.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<SearchWorksheetsResponse>): SearchWorksheetsResponse {
    const message = createBaseSearchWorksheetsResponse();
    message.worksheets = object.worksheets?.map((e) => Worksheet.fromPartial(e)) || [];
    message.nextPageToken = object.nextPageToken ?? "";
    return message;
  },
};

function createBaseWorksheet(): Worksheet {
  return {
    name: "",
    project: "",
    database: "",
    title: "",
    creator: "",
    createTime: undefined,
    updateTime: undefined,
    content: new Uint8Array(0),
    contentSize: Long.ZERO,
    visibility: Worksheet_Visibility.VISIBILITY_UNSPECIFIED,
    starred: false,
  };
}

export const Worksheet = {
  encode(message: Worksheet, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.name !== "") {
      writer.uint32(10).string(message.name);
    }
    if (message.project !== "") {
      writer.uint32(18).string(message.project);
    }
    if (message.database !== "") {
      writer.uint32(26).string(message.database);
    }
    if (message.title !== "") {
      writer.uint32(34).string(message.title);
    }
    if (message.creator !== "") {
      writer.uint32(42).string(message.creator);
    }
    if (message.createTime !== undefined) {
      Timestamp.encode(toTimestamp(message.createTime), writer.uint32(50).fork()).ldelim();
    }
    if (message.updateTime !== undefined) {
      Timestamp.encode(toTimestamp(message.updateTime), writer.uint32(58).fork()).ldelim();
    }
    if (message.content.length !== 0) {
      writer.uint32(66).bytes(message.content);
    }
    if (!message.contentSize.isZero()) {
      writer.uint32(72).int64(message.contentSize);
    }
    if (message.visibility !== Worksheet_Visibility.VISIBILITY_UNSPECIFIED) {
      writer.uint32(80).int32(worksheet_VisibilityToNumber(message.visibility));
    }
    if (message.starred === true) {
      writer.uint32(88).bool(message.starred);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): Worksheet {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseWorksheet();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag !== 10) {
            break;
          }

          message.name = reader.string();
          continue;
        case 2:
          if (tag !== 18) {
            break;
          }

          message.project = reader.string();
          continue;
        case 3:
          if (tag !== 26) {
            break;
          }

          message.database = reader.string();
          continue;
        case 4:
          if (tag !== 34) {
            break;
          }

          message.title = reader.string();
          continue;
        case 5:
          if (tag !== 42) {
            break;
          }

          message.creator = reader.string();
          continue;
        case 6:
          if (tag !== 50) {
            break;
          }

          message.createTime = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          continue;
        case 7:
          if (tag !== 58) {
            break;
          }

          message.updateTime = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
          continue;
        case 8:
          if (tag !== 66) {
            break;
          }

          message.content = reader.bytes();
          continue;
        case 9:
          if (tag !== 72) {
            break;
          }

          message.contentSize = reader.int64() as Long;
          continue;
        case 10:
          if (tag !== 80) {
            break;
          }

          message.visibility = worksheet_VisibilityFromJSON(reader.int32());
          continue;
        case 11:
          if (tag !== 88) {
            break;
          }

          message.starred = reader.bool();
          continue;
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): Worksheet {
    return {
      name: isSet(object.name) ? globalThis.String(object.name) : "",
      project: isSet(object.project) ? globalThis.String(object.project) : "",
      database: isSet(object.database) ? globalThis.String(object.database) : "",
      title: isSet(object.title) ? globalThis.String(object.title) : "",
      creator: isSet(object.creator) ? globalThis.String(object.creator) : "",
      createTime: isSet(object.createTime) ? fromJsonTimestamp(object.createTime) : undefined,
      updateTime: isSet(object.updateTime) ? fromJsonTimestamp(object.updateTime) : undefined,
      content: isSet(object.content) ? bytesFromBase64(object.content) : new Uint8Array(0),
      contentSize: isSet(object.contentSize) ? Long.fromValue(object.contentSize) : Long.ZERO,
      visibility: isSet(object.visibility)
        ? worksheet_VisibilityFromJSON(object.visibility)
        : Worksheet_Visibility.VISIBILITY_UNSPECIFIED,
      starred: isSet(object.starred) ? globalThis.Boolean(object.starred) : false,
    };
  },

  toJSON(message: Worksheet): unknown {
    const obj: any = {};
    if (message.name !== "") {
      obj.name = message.name;
    }
    if (message.project !== "") {
      obj.project = message.project;
    }
    if (message.database !== "") {
      obj.database = message.database;
    }
    if (message.title !== "") {
      obj.title = message.title;
    }
    if (message.creator !== "") {
      obj.creator = message.creator;
    }
    if (message.createTime !== undefined) {
      obj.createTime = message.createTime.toISOString();
    }
    if (message.updateTime !== undefined) {
      obj.updateTime = message.updateTime.toISOString();
    }
    if (message.content.length !== 0) {
      obj.content = base64FromBytes(message.content);
    }
    if (!message.contentSize.isZero()) {
      obj.contentSize = (message.contentSize || Long.ZERO).toString();
    }
    if (message.visibility !== Worksheet_Visibility.VISIBILITY_UNSPECIFIED) {
      obj.visibility = worksheet_VisibilityToJSON(message.visibility);
    }
    if (message.starred === true) {
      obj.starred = message.starred;
    }
    return obj;
  },

  create(base?: DeepPartial<Worksheet>): Worksheet {
    return Worksheet.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<Worksheet>): Worksheet {
    const message = createBaseWorksheet();
    message.name = object.name ?? "";
    message.project = object.project ?? "";
    message.database = object.database ?? "";
    message.title = object.title ?? "";
    message.creator = object.creator ?? "";
    message.createTime = object.createTime ?? undefined;
    message.updateTime = object.updateTime ?? undefined;
    message.content = object.content ?? new Uint8Array(0);
    message.contentSize = (object.contentSize !== undefined && object.contentSize !== null)
      ? Long.fromValue(object.contentSize)
      : Long.ZERO;
    message.visibility = object.visibility ?? Worksheet_Visibility.VISIBILITY_UNSPECIFIED;
    message.starred = object.starred ?? false;
    return message;
  },
};

export type WorksheetServiceDefinition = typeof WorksheetServiceDefinition;
export const WorksheetServiceDefinition = {
  name: "WorksheetService",
  fullName: "bytebase.v1.WorksheetService",
  methods: {
    /** Create a personal worksheet used in SQL Editor. */
    createWorksheet: {
      name: "CreateWorksheet",
      requestType: CreateWorksheetRequest,
      requestStream: false,
      responseType: Worksheet,
      responseStream: false,
      options: {
        _unknownFields: {
          8410: [new Uint8Array([16, 112, 97, 114, 101, 110, 116, 44, 119, 111, 114, 107, 115, 104, 101, 101, 116])],
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              27,
              58,
              9,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              34,
              14,
              47,
              118,
              49,
              47,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
            ]),
          ],
        },
      },
    },
    /**
     * Get a worksheet by name.
     * The users can access this method if,
     * - they are the creator of the worksheet;
     * - they have bb.worksheets.get permission on the workspace;
     * - the sheet is shared with them with PROJECT_READ and PROJECT_WRITE visibility, and they have bb.projects.get permission on the project.
     */
    getWorksheet: {
      name: "GetWorksheet",
      requestType: GetWorksheetRequest,
      requestStream: false,
      responseType: Worksheet,
      responseStream: false,
      options: {
        _unknownFields: {
          8410: [new Uint8Array([4, 110, 97, 109, 101])],
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              25,
              18,
              23,
              47,
              118,
              49,
              47,
              123,
              110,
              97,
              109,
              101,
              61,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
              47,
              42,
              125,
            ]),
          ],
        },
      },
    },
    /**
     * Search for worksheets.
     * This is used for finding my worksheets or worksheets shared by other people.
     * The sheet accessibility is the same as GetWorksheet().
     */
    searchWorksheets: {
      name: "SearchWorksheets",
      requestType: SearchWorksheetsRequest,
      requestStream: false,
      responseType: SearchWorksheetsResponse,
      responseStream: false,
      options: {
        _unknownFields: {
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              26,
              58,
              1,
              42,
              34,
              21,
              47,
              118,
              49,
              47,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
              58,
              115,
              101,
              97,
              114,
              99,
              104,
            ]),
          ],
        },
      },
    },
    /**
     * Update a worksheet.
     * The users can access this method if,
     * - they are the creator of the worksheet;
     * - they have bb.worksheets.manage permission on the workspace;
     * - the sheet is shared with them with PROJECT_WRITE visibility, and they have bb.projects.get permission on the project.
     */
    updateWorksheet: {
      name: "UpdateWorksheet",
      requestType: UpdateWorksheetRequest,
      requestStream: false,
      responseType: Worksheet,
      responseStream: false,
      options: {
        _unknownFields: {
          8410: [
            new Uint8Array([
              21,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              44,
              117,
              112,
              100,
              97,
              116,
              101,
              95,
              109,
              97,
              115,
              107,
            ]),
          ],
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              46,
              58,
              9,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              50,
              33,
              47,
              118,
              49,
              47,
              123,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              46,
              110,
              97,
              109,
              101,
              61,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
              47,
              42,
              125,
            ]),
          ],
        },
      },
    },
    /**
     * Update the organizer of a worksheet.
     * The access is the same as UpdateWorksheet method.
     */
    updateWorksheetOrganizer: {
      name: "UpdateWorksheetOrganizer",
      requestType: UpdateWorksheetOrganizerRequest,
      requestStream: false,
      responseType: WorksheetOrganizer,
      responseStream: false,
      options: {
        _unknownFields: {
          8410: [
            new Uint8Array([
              21,
              111,
              114,
              103,
              97,
              110,
              105,
              122,
              101,
              114,
              44,
              117,
              112,
              100,
              97,
              116,
              101,
              95,
              109,
              97,
              115,
              107,
            ]),
          ],
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              61,
              58,
              9,
              111,
              114,
              103,
              97,
              110,
              105,
              122,
              101,
              114,
              50,
              48,
              47,
              118,
              49,
              47,
              123,
              111,
              114,
              103,
              97,
              110,
              105,
              122,
              101,
              114,
              46,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              61,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
              47,
              42,
              125,
              47,
              111,
              114,
              103,
              97,
              110,
              105,
              122,
              101,
              114,
            ]),
          ],
        },
      },
    },
    /**
     * Delete a worksheet.
     * The access is the same as UpdateWorksheet method.
     */
    deleteWorksheet: {
      name: "DeleteWorksheet",
      requestType: DeleteWorksheetRequest,
      requestStream: false,
      responseType: Empty,
      responseStream: false,
      options: {
        _unknownFields: {
          8410: [new Uint8Array([4, 110, 97, 109, 101])],
          800016: [new Uint8Array([2])],
          578365826: [
            new Uint8Array([
              25,
              42,
              23,
              47,
              118,
              49,
              47,
              123,
              110,
              97,
              109,
              101,
              61,
              119,
              111,
              114,
              107,
              115,
              104,
              101,
              101,
              116,
              115,
              47,
              42,
              125,
            ]),
          ],
        },
      },
    },
  },
} as const;

function bytesFromBase64(b64: string): Uint8Array {
  if (globalThis.Buffer) {
    return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
  } else {
    const bin = globalThis.atob(b64);
    const arr = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; ++i) {
      arr[i] = bin.charCodeAt(i);
    }
    return arr;
  }
}

function base64FromBytes(arr: Uint8Array): string {
  if (globalThis.Buffer) {
    return globalThis.Buffer.from(arr).toString("base64");
  } else {
    const bin: string[] = [];
    arr.forEach((byte) => {
      bin.push(globalThis.String.fromCharCode(byte));
    });
    return globalThis.btoa(bin.join(""));
  }
}

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
  : T extends Long ? string | number | Long : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
  : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
  : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
  : Partial<T>;

function toTimestamp(date: Date): Timestamp {
  const seconds = numberToLong(date.getTime() / 1_000);
  const nanos = (date.getTime() % 1_000) * 1_000_000;
  return { seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
  let millis = (t.seconds.toNumber() || 0) * 1_000;
  millis += (t.nanos || 0) / 1_000_000;
  return new globalThis.Date(millis);
}

function fromJsonTimestamp(o: any): Date {
  if (o instanceof globalThis.Date) {
    return o;
  } else if (typeof o === "string") {
    return new globalThis.Date(o);
  } else {
    return fromTimestamp(Timestamp.fromJSON(o));
  }
}

function numberToLong(number: number) {
  return Long.fromNumber(number);
}

if (_m0.util.Long !== Long) {
  _m0.util.Long = Long as any;
  _m0.configure();
}

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}
